package cn.ilovese.collect.controller.user;

import cn.ilovese.collect.service.user.MessageService;
import cn.ilovese.collect.service.user.UserService;
import cn.ilovese.collect.util.Constant;
import cn.ilovese.collect.vo.InviteGroupVO;
import cn.ilovese.collect.vo.ResultVO;
import cn.ilovese.collect.vo.follow.FollowGroupVO;
import cn.ilovese.collect.vo.task.TaskVO;
import cn.ilovese.collect.vo.user.AccountLoginVO;
import cn.ilovese.collect.vo.user.EmailLoginVO;
import cn.ilovese.collect.vo.user.UserVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @Resource
    MessageService messageService;
    //三个注解配置接收前端调用时传给的参数，你们自己根据需要给每个Controller方法的参数添加
    //@RequestBody，接收前端传递给后端的json字符串中的数据的(请求体中的数据的)
    //@RequestParam，将请求参数绑定到你控制器的方法参数上（是springmvc中接收普通参数的注解）
    //@PathVariable，接收请求路径中占位符的值

    // 注册，目前还不知道是对应前端哪个路由，用的get还是post方法，先都用post替代
    @PostMapping("/1")
    ResultVO<UserVO> userRegister(@RequestBody UserVO vo){
        System.out.println(vo.getUsername());
        System.out.println(vo.getPassword());
        System.out.println(vo.getEmail());
        System.out.println(vo.getRole());
        return userService.userRegister(vo);
    }

    // 登录，目前还不知道是对应前端哪个路由，用的get还是post方法，先都用post替代
    @PostMapping("/2")
    ResultVO<UserVO> userLogin(@RequestBody AccountLoginVO accountLoginVO){
        ResultVO<UserVO> result=userService.userLogin(accountLoginVO.getUsername(),accountLoginVO.getPassword());

        UserVO temp=result.getData();
        if(result.getCode() == 200){
            System.out.println(messageService.getMessageNums(
                    result.getData().getId()));
            temp.setMessageNum(messageService.getMessageNums(
                    result.getData().getId()));
            result.setData(temp);
        }

        return result;
    }

    //TODO
    @PostMapping("/3")
    ResultVO<UserVO> userLoginWithEmail(@RequestBody EmailLoginVO emailLoginVO){
        ResultVO<UserVO> result=userService.userLoginWithEmail(emailLoginVO.getEmail(),emailLoginVO.getPassword());
        UserVO temp=result.getData();
        if(result.getCode() == 200){
            temp.setMessageNum(messageService.getMessageNums(
                    result.getData().getId()));
            result.setData(temp);
        }
        return result;
    }

    
    // 获得指定id的User，目前还不知道是对应前端哪个路由，用的get还是post方法，先都用post替代
    @GetMapping("/4")
    UserVO getUser(Integer uid){
        return userService.getUser(uid);
    }

    @PostMapping("/5")
    ResultVO<UserVO> updateUserInfo(@RequestBody UserVO vo){
        System.out.println(vo.getUsername());
        System.out.println(vo.getPassword());
        System.out.println(vo.getEmail());
        System.out.println(vo.getRole());
        return userService.updateUserInfo(vo);
    }

    @GetMapping("/getActivityCount/{workerid}")
    public ResultVO<Map<String,Object>> getActivityCount(@PathVariable Integer workerid){
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取活跃度列表成功",userService.getContributeCount(workerid));
    }

    @GetMapping("/getWorkers")
    ResultVO<List<UserVO>> getAllWorkers(){
        return userService.getAllWorkers();
    }

    @PostMapping("/invite")
    ResultVO<Integer> inviteWorkers(@RequestBody InviteGroupVO inviteGroupVO){
        return userService.inviteWorkers(inviteGroupVO);
    }

    @GetMapping("/getInvitedTasks/{workerid}")
    ResultVO<List<TaskVO>> getInvitedTaskByWorkerId(@PathVariable Integer workerid){
        return userService.getInvitedTaskByWorkerId(workerid);
    }
}
